package com.jlastu.dlxy_androidapp.view.details;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.jlastu.dlxy_androidapp.R;
import com.jlastu.dlxy_androidapp.adapter.GoodsListAdapter;
import com.jlastu.dlxy_androidapp.adapter.StoreGoodsListAdapter;
import com.jlastu.dlxy_androidapp.contract.StoreContract;
import com.jlastu.dlxy_androidapp.entity.Goods;
import com.jlastu.dlxy_androidapp.entity.OrderCart;
import com.jlastu.dlxy_androidapp.entity.Photo;
import com.jlastu.dlxy_androidapp.entity.Store;
import com.jlastu.dlxy_androidapp.presenter.StorePresenter;
import com.jlastu.dlxy_androidapp.utils.SpUtils;

import java.util.ArrayList;
import java.util.List;

public class StoreActivity extends AppCompatActivity implements StoreContract.IStoreView {
    private RecyclerView mGoodsRecyclerView;
    private StoreGoodsListAdapter mRecyclerAdapter;

    private StoreContract.IStorePresenter mStorePresenter;
    private SpUtils mSpUtils;
    Button customer_service_button;
    private List<Goods> mGoodsList;

    private List<Photo> mPhotoList;
    private TextView mStoreName;
    ImageView back_button;
    private int store_id;


    ArrayList<OrderCart> ordercartsList = new ArrayList<>();

    @SuppressLint({"WrongViewCast", "MissingInflatedId"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store);
        mStorePresenter = new StorePresenter(this, this);
        mStoreName = findViewById(R.id.shop_name);
        mGoodsList = new ArrayList<>();
        mPhotoList = new ArrayList<>();
        mGoodsRecyclerView = findViewById(R.id.rv_goods);
        StaggeredGridLayoutManager staggeredGridLayoutManager =
                new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        mGoodsRecyclerView.setLayoutManager(staggeredGridLayoutManager);
        mRecyclerAdapter = new StoreGoodsListAdapter(mGoodsList, mPhotoList);
        mGoodsRecyclerView.setAdapter(mRecyclerAdapter);

        mSpUtils = new SpUtils(getApplicationContext());

        Intent intent = getIntent();
        store_id = intent.getIntExtra("store_id",0);
        if (intent != null) {
            ordercartsList = (ArrayList<OrderCart>) intent.getSerializableExtra("ordercartsList");
        }

        //客服
        customer_service_button = findViewById(R.id.customer_service_button);
        customer_service_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(StoreActivity.this, Service.class);
                startActivity(intent);
            }
        });
        //返回按钮
        back_button = findViewById(R.id.back_button);
        back_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
                finish();
            }
        });

    }

    @Override
    public void onResume() {
        super.onResume();
        //查询当前登录帐号的id
        int user_id = mSpUtils.getInt("userid", 0);
        if (store_id != 0) {
            //根据id查询当前登录用户的信息
            mStorePresenter.getStore(store_id);
            mStorePresenter.getGoodsList(store_id);
        }else {
            mStorePresenter.getStore(user_id);
            mStorePresenter.getGoodsList(user_id);
        }
    }

    public void toggleFollow(View view) {
        Button followButton = (Button) view;
        String buttonText = followButton.getText().toString();
        boolean isFollowing = buttonText.equals("已关注");

        if (isFollowing) {
            buttonText = "关注";
            Toast.makeText(StoreActivity.this, "取消关注", Toast.LENGTH_SHORT).show();
            // 取消关注逻辑
        } else {
            Toast.makeText(StoreActivity.this, "关注成功", Toast.LENGTH_SHORT).show();
            buttonText = "已关注";
            // 关注逻辑
        }

        followButton.setText(buttonText);
    }

    @Override
    public void showStore(Store store) {
        mStoreName.setText(store.getStore_name());
    }

    @Override
    public void showGoodsList(List<Goods> goodsList, List<Photo> photoList) {
        mGoodsList = goodsList;
        mPhotoList = photoList;
        Log.d("", "showGoodsList " + mGoodsList.size());
        mRecyclerAdapter.updateData(mGoodsList, mPhotoList);
    }
}